package com.imooc.service.impl.center;

import com.github.pagehelper.PageHelper;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.ItemsCommentsMapperCustom;
import com.imooc.mapper.OrderItemsMapper;
import com.imooc.mapper.OrderStatusMapper;
import com.imooc.mapper.OrdersMapper;
import com.imooc.pojo.OrderItems;
import com.imooc.pojo.OrderStatus;
import com.imooc.pojo.Orders;
import com.imooc.pojo.bo.center.OrderItemsCommentBO;
import com.imooc.pojo.vo.MyCommentsVO;
import com.imooc.service.center.MyCommentsService;
import com.imooc.service.impl.BaseService;
import com.imooc.utils.PagedGridResult;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: CenterUserServiceImpl
 * @Package: com.imooc.service.impl
 * @Description:
 * @Date: 2021/7/5 11:26
 * @Author: sunzheng@beiming.com
 */
@Service
public class MyCommentsServiceImpl extends BaseService implements MyCommentsService {

  @Autowired
  private OrderItemsMapper orderItemsMapper;

  @Autowired
  private OrdersMapper ordersMapper;

  @Autowired
  private OrderStatusMapper orderStatusMapper;

  @Autowired
  private Sid sid;

  @Autowired
  private ItemsCommentsMapperCustom itemsCommentsMapperCustom;

  @Transactional(propagation = Propagation.REQUIRED)
  @Override
  public void saveComments(String userId, String orderId, List<OrderItemsCommentBO> commentList) {
    // 1.保存评价 items_comments
    for (OrderItemsCommentBO oic : commentList) {
      oic.setCommentId(sid.nextShort());
    }
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    map.put("commentList", commentList);
    itemsCommentsMapperCustom.saveComments(map);

    // 2.修改订单表，改为已评论 orders
    Orders order = new Orders();
    order.setId(orderId);
    order.setIsComment(YesOrNo.YES.type);
    ordersMapper.updateByPrimaryKeySelective(order);

    // 3.修改订单状态表的留言时间 order_status
    OrderStatus orderStatus = new OrderStatus();
    orderStatus.setOrderId(orderId);
    orderStatus.setCommentTime(new Date());
    orderStatusMapper.updateByPrimaryKeySelective(orderStatus);

  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult queryMyComments(String userId, Integer page, Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);

    PageHelper.startPage(page, pageSize);

    List<MyCommentsVO> list = itemsCommentsMapperCustom.queryMyComments(map);
    return setPageGrid(list, page);
  }

  @Transactional(propagation = Propagation.SUPPORTS)
  @Override
  public List<OrderItems> queryPendingComment(String orderId) {
    OrderItems query = new OrderItems();
    query.setOrderId(orderId);
    return orderItemsMapper.select(query);
  }
}
